package org.flexthinker.binding.ui.form.binding.support
{
    import flash.display.DisplayObject;

    import org.flexthinker.binding.bean.AutoBinding;
    import org.flexthinker.binding.bean.BeanProperty;

    import org.flexthinker.binding.bean.Binding;
    import org.flexthinker.binding.bean.FormModelProperty;
    import org.flexthinker.binding.bean.UpdateStrategy;

    /**
     * Basic <code>Binder</code> implementation for visual components.
     * <p>
     *     By default, this Binder creates a read-only binding between the
     *     <code>formProperty</code> of the source form model and the "text" property of the <code>component</code>.
     * </p>
     */
    public class FieldBinder extends AbstractBinder
    {
        private var binding:Binding;

        /**
         * Update strategy used for <code>Binding</code>.
         * <p>
         *     Default value is: <code>UpdateStrategy.READ</code>
         * </p>
         */
        protected var updateStrategy:UpdateStrategy = UpdateStrategy.READ;

        /**
         * The <code>component</code>'s property to bind to.
         * <p>
         *     Default value is: <code>text</code>
         * </p>
         */
        protected var componentProperty:String = "text";

        /**
         *
         * @param component
         *          The visual component to bind to the given <code>formProperty</code>
         * @param formProperty
         *          The FormModel property to be bounded to the <code>component</code>
         */
        public function FieldBinder(component:DisplayObject, formProperty:String)
        {
            super(component, formProperty);
        }

        // --------------------
        //   internal methods
        // --------------------

        override protected function getAutoBinding():Binding
        {
            if ( !binding )
            {
                binding = new AutoBinding(updateStrategy,
                                        getFormModel(), FormModelProperty.create(getFormProperty()),
                                        getComponent(), BeanProperty.create(componentProperty), getFormProperty());
            }
            return binding;
        }
    }
}
